import React, { useEffect } from 'react'
import echarts from 'echarts/lib/echarts'
import 'echarts/lib/chart/bar'
import 'echarts/lib/component/tooltip'
import 'echarts/lib/component/title'
import 'echarts/lib/component/legend'
import { TYPE, STATE } from '@/const'
const Bar = ({ newsCount }) => {
    let type = Object.keys(newsCount).map(v => {
        return TYPE[v]
    })

    let data = Object.values(newsCount)
    useEffect(() => {
        let myChart = echarts.init(document.getElementById('bar2'))
        myChart.setOption({
            title: {
                text: '近七天增长量'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: [
                {
                    type: 'category',
                    data: type
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: [
                {
                    name: '近七天发布量',
                    type: 'bar',
                    data: data,
                    barWidth: '20%',
                    itemStyle: {
                        normal: {
                            color: function(params) {
                                //自定义颜色
                                var colorList = (colorList = [
                                    '#660099',
                                    '#CC00CC',
                                    '#CC0066',
                                    '#9966FF',
                                    '#9900CC',
                                    '#990033'
                                ])
                                return colorList[params.dataIndex]
                            }
                        }
                    }
                }
            ]
        })
        window.addEventListener('resize', function() {
            myChart.resize()
        })
    }, [type, data])
    return <div id='bar2' style={{ height: 300, background: '#fff' }}></div>
}

export default Bar
